import os
import subprocess
import time
from tools import save_to_config, read_from_config

def ping_ip(ip):
    timeout = read_from_config("timeout")  # 获取超时时间
    start_time = time.time()
    param = '-n' if os.name == 'nt' else '-c'  # 判断操作系统
    while time.time() - start_time < timeout:  # 使用自定义的超时时间
        response = subprocess.run(['ping', param, '1', ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if response.returncode == 0:
            return True
        time.sleep(1)  # 每秒ping一次
    return False

def update_countdown(countdown_time, countdown_label, popup):
    """更新倒计时标签."""
    for i in range(countdown_time, 0, -1):
        if popup.winfo_exists():  # 检查弹窗是否存在
            countdown_label.config(text=f"{i}秒")
            countdown_label.update()
            time.sleep(1)
        else:
            break
    if popup.winfo_exists():
        countdown_label.config(text="时间到")
